package demo.using.threadPool;

/**
 * @author : GuoFei
 * @since : 2019/7/4 15:55
 */
public class TestMain3 {

    public static void main(String[] args) throws InterruptedException {
        // 添加线程，先锁住该测试类2秒
        new Thread(() -> {
            synchronized (TestMain3.class) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // 添加测试线程
        Thread thread = new Thread(() -> {
            synchronized (TestMain3.class) {
                // 【1】打印自己的状态为：运行 RUNNABLE
                TestMain3.printState(Thread.currentThread(),1);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 【2】此时测试线程状态为新建：NEW
        printState(thread,2);
        thread.start();
        Thread.sleep(1000);
        // 【3】此时测试线程拿不到锁，状态3为锁定：BLOCKED
        printState(thread,3);
        Thread.sleep(2000);
        // 【4】此时测试线程拿到了锁，在自己的休眠状态：TIMED_WAITING
        printState(thread,4);
        Thread.sleep(2000);
        // 【5】此时测试线程已经运行完毕，状态为终止：TERMINATED
        printState(thread,5);


    }

    // 打印当前线程状态
    private static void printState(Thread thread,int index) {
        System.out.println("["+index + "] : " + thread.getName() + " : state : " + thread.getState().toString());
    }

}
